/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iss.examsys.view;

import com.iss.examsys.ejb.ModuleBean;
import com.iss.examsys.entity.Module;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.inject.Named;

/**
 *
 * @author Quek Ting Hong
 */
@RequestScoped
@Named
public class ModuleView {
    @EJB ModuleBean moduleBean;
    
    private Long moduleId;
    private Module selectedModule = new Module();
    private List<Module> selectedModules;
    
    private Module newModule = new Module();
    private List<Module> allModule;

    public Long getModuleId() {
        return moduleId;
    }

    public void setModuleId(Long moduleId) {
        this.moduleId = moduleId;
    }

    public Module getSelectedModule() {
        return selectedModule;
    }

    public void setSelectedModule(Module selectedModule) {
        this.selectedModule = selectedModule;
    }

    public Module getNewModule() {
        return newModule;
    }

    public void setNewModule(Module newModule) {
        this.newModule = newModule;
    }

    public List<Module> getAllModule() {
        setAllModule(moduleBean.findAllModule());
        return allModule;
    }

    public void setAllModule(List<Module> allModule) {
        this.allModule = allModule;
    }

    public List<Module> getSelectedModules() {
        return selectedModules;
    }

    public void setSelectedModules(List<Module> selectedModules) {
        this.selectedModules = selectedModules;
    }
    
    
    public void createModule(){
        System.out.println("Create module : " +newModule.toString());
               
        
        moduleBean.createModule(newModule);
        
    }
    
    public void validateModuleCode(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        
        UIInput inputId = (UIInput)component.findComponent("id");
        
        List<Module> modules;
                
        if(inputId!=null){
            Long moduleId = Long.parseLong((inputId.getLocalValue().toString()));
            modules = moduleBean.findModuleByCode((String)value, moduleId);
        }
        else{
            modules = moduleBean.findModuleByCode((String)value);
        }
        
         
        
        if (!modules.isEmpty()) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Module code already in use, choose another", null));
        }
    }
    
    public void deleteModule(){
        System.out.println(getSelectedModules());
        
        moduleBean.deleteModule(selectedModules);
        
    }
    
    public void editModule(){
        System.out.println(getSelectedModule().getId()+", "+getSelectedModule().getCode()+", "+getSelectedModule().getName());
        
        moduleBean.editModule(getSelectedModule());
    }
}
